package com.store.app.http;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

//import com.store.app.database.Database;
//import com.store.app.database.DatabaseMethod;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Stores extends Thread {

	public static String member_id,app_token,stores_name,stores_type_key,contact_person,area_id,store_desc;
	public static String contact_tel,address,neighbor_pic_path,longitude,latitude,activity_servcie,area_desc;
	public static String business_type_key;//门店商业类型
//	public static String http = "http://192.168.16.240";				//内部
//	public static String http = "http://t-appportal.meitianhui.com";	//测试
	public static String http = "http://appportal.meitianhui.com";		//正式生产环境


	public static double cash_balance;                // 零钱
	public static double total_cash_commission;       // 佣金
	public static String gold = "";                   // 金币
	public static int voucher_balance;                // 礼券余额

	public static List<Integer> type_service = new ArrayList<Integer>();
	public static List<String> listposters = new ArrayList<String>();


	// public static DatabaseMethod method;
	// Database database;
	private   Context context;
	public Stores( String app_token,String member_id,Context context) {
		Log.v("zyl","Stores==============");
		Stores.app_token = app_token;
		Stores.member_id = member_id;
		this.context=context;
	}
    public void setDatabase(){
		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
		area_id = preferences.getString("area_id", "");
		stores_name = preferences.getString("stores_name", "");
		stores_type_key = preferences.getString("stores_type_key", "");
		business_type_key = preferences.getString("business_type_key", "");
		contact_person = preferences.getString("contact_person", "");
		contact_tel = preferences.getString("contact_tel", "");
		if(TextUtils.isEmpty(address)) {
			address = preferences.getString("address", "");
		}
		if(TextUtils.isEmpty(area_desc)) {
			area_desc = preferences.getString("area_desc", "");
		}
		neighbor_pic_path = preferences.getString("neighbor_pic_path", "");
		longitude = preferences.getString("longitude", "");
		latitude = preferences.getString("latitude", "");
//		activity_servcie= preferences.getString("activity_servcie", "");
//		Log.v("zyl","转换："+activity_servcie);
//		if(!activity_servcie.equals("")){
//			if(type_service!=null){
//				if(type_service.size()==0){
//					String[] sourceStrArray = activity_servcie.split(",");
//					for(int i=0;i<sourceStrArray.length;i++){
//						type_service.add(Integer.valueOf(sourceStrArray[i]).intValue());//Integer.parseInt(sourceStrArray[i]));
//					}
//				}
//			}
//		}
		store_desc= preferences.getString("store_desc", "");
		Log.v("zyl","==area_desc:"+area_desc);


	}
	public void run() {

		try {
			String result="";
			JSONObject obj=null;
			if(!TextUtils.isEmpty(Stores.member_id)) {
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("app_token", app_token));
				JSONObject paramsone = new JSONObject();
				paramsone.put("member_id", member_id);
				params.add(new BasicNameValuePair("params", paramsone.toString()));
				params.add(new BasicNameValuePair("service", "member.storeFind"));
				String sign = Submit.genPackageSign(params);
				params.add(new BasicNameValuePair("sign", sign));
				obj = Submit.submit(params, http + "/openapi/member");
				Log.i("mylog", "ssss " + obj.toString());
				result=obj.getString("rsp_code");
			}
			if(result.equals("succ")){
				JSONObject data=obj.getJSONObject("data");
				area_id= data.getString("area_id");//配送范围
				stores_name= data.getString("stores_name");//加盟店名称
				Log.v("zyl","加盟店名称："+stores_name);
				stores_type_key= data.getString("stores_type_key");//加盟店商业类别
				business_type_key = data.getString("business_type_key");//门店商业类型
				contact_person= data.getString("contact_person");//联系人(店主)
				contact_tel= data.getString("contact_tel");//联系电话
				address= data.getString("address");//详细地址
				Log.v("zyl","重新获取地址："+address);
				neighbor_pic_path= data.getString("neighbor_pic_path");
				longitude= data.getString("longitude");//经度
				latitude= data.getString("latitude");//维度
				store_desc = data.getString("desc1");//描述
				area_desc = data.getString("area_desc");
				area_desc = area_desc.replace("中国,", "").replace(",","");
				activity_servcie = data.getString("activity_servcie");//提供的服务
				Log.v("zyl","---:"+activity_servcie);
				if(!TextUtils.isEmpty(activity_servcie)){
					activity_servcie=activity_servcie.substring(1, activity_servcie.length() - 1);
					activity_servcie=activity_servcie.replaceAll(" ", "");
				}

				if(!TextUtils.isEmpty(Stores.activity_servcie)){
					if(type_service!=null){
						if(type_service.size()==0){
					String[] sourceStrArray = activity_servcie.split(",");
					for(int i=0;i<sourceStrArray.length;i++){
						type_service.add(Integer.valueOf(sourceStrArray[i]).intValue());//Integer.parseInt(sourceStrArray[i]));
					}
						}
					}
				}

//				if(!activity_posters.equals("")){
//					activity_posters=activity_posters.substring(1,activity_posters.length()-1);
//					activity_posters=activity_posters.replaceAll(" ", "");
//					String[] postersStrArray = activity_posters.split(",");
//					for(int i=0;i<postersStrArray.length;i++){
//						type_service.add(Integer.valueOf(postersStrArray[i]).intValue());//Integer.parseInt(sourceStrArray[i]));
//					}
//				}

				/*database = new Database(MainActivity.context);
				method=new DatabaseMethod(database);
				List<String> privilege= method.privilege();
				subtract=Integer.parseInt(privilege.get(0));*/
				//保存店铺资料
				SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
				SharedPreferences.Editor editor = preferences.edit();
				editor.putString("member_id", member_id).apply();
				editor.putString("area_id", area_id).apply();
				editor.putString("area_desc", area_desc).apply();
				editor.putString("stores_name", stores_name).apply();
				editor.putString("stores_type_key", stores_type_key).apply();
				editor.putString("business_type_key", business_type_key).apply();
				editor.putString("contact_person", contact_person).apply();
				editor.putString("contact_tel", contact_tel).apply();
				editor.putString("address", address).apply();
				editor.putString("neighbor_pic_path", neighbor_pic_path).apply();
				editor.putString("longitude", longitude).apply();
				editor.putString("latitude", latitude).apply();
				editor.putString("activity_servcie", activity_servcie).apply();
				editor.putString("store_desc", store_desc).apply();
			}else{
				if(context!=null) {
					SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
					if (!TextUtils.isEmpty(member_id) && member_id.equals(preferences.getString("member_id", ""))) {
						area_desc = preferences.getString("area_desc","");
						area_id = preferences.getString("area_id", "");
						stores_name = preferences.getString("stores_name", "");
						stores_type_key = preferences.getString("stores_type_key", "");
						business_type_key = preferences.getString("business_type_key", "");
						contact_person = preferences.getString("contact_person", "");
						contact_tel = preferences.getString("contact_tel", "");
						address = preferences.getString("address", "");
						neighbor_pic_path = preferences.getString("neighbor_pic_path", "");
						longitude = preferences.getString("longitude", "");
						latitude = preferences.getString("latitude", "");
						store_desc = preferences.getString("store_desc", "");
						activity_servcie= preferences.getString("activity_servcie", "");
						if(!activity_servcie.equals("")){
							if(type_service!=null){
								if(type_service.size()==0){
							String[] sourceStrArray = activity_servcie.split(",");
							for(int i=0;i<sourceStrArray.length;i++){
								type_service.add(Integer.valueOf(sourceStrArray[i]).intValue());//Integer.parseInt(sourceStrArray[i]));
							}
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			if(context!=null){
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			if(!TextUtils.isEmpty(member_id)&&TextUtils.isEmpty(area_id)&&member_id.equals(preferences.getString("member_id", ""))){
				area_id = preferences.getString("area_id", "");
				area_desc = preferences.getString("area_desc", "");
				stores_name = preferences.getString("stores_name", "");
				stores_type_key = preferences.getString("stores_type_key", "");
				business_type_key = preferences.getString("business_type_key", "");
				contact_person = preferences.getString("contact_person", "");
				contact_tel = preferences.getString("contact_tel", "");
				address = preferences.getString("address", "");
				neighbor_pic_path = preferences.getString("neighbor_pic_path", "");
				longitude = preferences.getString("longitude", "");
				latitude = preferences.getString("latitude", "");
				store_desc = preferences.getString("store_desc", "");
				activity_servcie= preferences.getString("activity_servcie", "");
				if(!activity_servcie.equals("")){
					if(type_service!=null){
						if(type_service.size()==0){
							String[] sourceStrArray = activity_servcie.split(",");
							for(int i=0;i<sourceStrArray.length;i++){
								type_service.add(Integer.valueOf(sourceStrArray[i]).intValue());//Integer.parseInt(sourceStrArray[i]));
							}
						}
					}
				}
			}
			}
		}

	}
}
